# coding:utf-8
# @Time    : 2021/11/29 17:38
# @Author  : Martin

from page_object.base import PageItems
from util.excel_write import write_excel
from util.read_file import get_approve_data


class OrderOptionPackage(PageItems):
    """
    订单自选搭配活动
    """
    _base_url = "/nspas-web-in/processRoleFilter.do?businessTemplateId=NSOA22714&comType" \
                "=1&promotionActType=ORDEROPTIONALPACKAGE"

    def input_discount_rule(self, args: list):

        """
        输入满类打折
        al_type: 计算方式 满=3 每=2 第=4
        cal_limit: 计算门槛值
        unit: 计算单位 元=1 件=3
        amount: 优惠金额
        max_amount: 最大优惠金额
        :param rule_type: 1:满类减元 3:满类打折
        :param args: 参数列表
        """

        for arg in args:
            cal_type, cal_limit, unit, mode, amount = arg.split(',')
            table = self.locate('css selector', '#actRuleAndLimit')
            self.scroll_ele(table)
            index = args.index(arg) + 1

            # 如果有1大于第一层级的优惠，且当前不是最后一个层级，点击添加按钮
            if index > 1 and index != len(args)-1:
                self.click('xpath', '//a[@onclick="selfCombAdd()"]')

            self.input('css selector', f'#detailinfoOrderSelfCombine>#detailedtableOrderSelfCombine:nth-child({index})'
                                       ' input[name="orderSelfComb.preferSill"]', cal_limit)
            self.input('css selector', f'#detailinfoOrderSelfCombine>#detailedtableOrderSelfCombine:nth-child({index})'
                                       ' input[name="orderSelfComb.preferType"]', amount)

    def input_reduce_rule(self, args: list):
        """
        输入满类减元
        al_type: 计算方式 满=3 每=2 第=4
        cal_limit: 计算门槛值
        unit: 计算单位 元=1 件=3
        amount: 优惠金额
        max_amount: 最大优惠金额
        :param rule_type: 1:满类减元 3:满类打折
        :param args: 参数列表
        """
        self.click('xpath', '//input[@name="ruleOption"][@value="1"]')
        for arg in args:
            cal_type, cal_limit, unit, mode, amount = arg.split(',')
            table = self.locate('css selector', '#actRuleAndLimit')
            self.scroll_ele(table)
            index = args.index(arg) + 1

            # 点击添加按钮
            self.click('xpath', '//a[@onclick="selfCombAdd()"]')

            self.input('css selector', f'#detailinfoOrderSelfCombine>#detailedtableOrderSelfCombine:nth-child({index})'
                                       ' input[name="orderSelfComb.preferSill"]', cal_limit)
            self.input('css selector', f'#detailinfoOrderSelfCombine>#detailedtableOrderSelfCombine:nth-child({index})'
                                       ' input[name="orderSelfComb.preferType"]', amount)

    def input_max_amount(self, txt):
        """
        输入最大金额
        :param txt:  最大金额
        :return:
        """
        max_amount = str(txt).split(',')
        for maxi in max_amount:
            index = max_amount.index(maxi) + 1
            self.input('css selector', f'#detailinfoOrderSelfCombine>#detailedtableOrderSelfCombine:nth-child({index})'
                                       ' input[name="orderSelfComb.ticketPriceMax"]', maxi)

    def import_act_scope(self, params):
        index = 0
        for param in params:
            params[index] = param.split('_')
            index += 1

        path = '/excel_template/orderOptionalPackageScopeTemplate.xls'
        write_excel(path, params)
        # singleCateFile
        self.upload('id', 'singleCateFile', path, event="onchange")
        self.click('xpath', '//a[@onclick="importActivityScopeOOP()"]')
        # 确认导入结果 '本次导入数据2条数据，成功2条，重复0条！'
        # self.switch_alert()
        return self.switch_alert()

    @staticmethod
    def get_approve_head_create():
        """
        获取总部新建审批流账号数据
        :return:
        """
        return get_approve_data('online_order_reduce', 'head', 'create')


if __name__ == '__main__':
    pass
